package com.cym.controller;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cym.config.AdminInterceptor;
import com.cym.ext.GroupExt;
import com.cym.ext.RepositoryExt;
import com.cym.ext.RepositoryGroupExt;
import com.cym.ext.RepositoryUserExt;
import com.cym.ext.UserExt;
import com.cym.model.Group;
import com.cym.model.Repository;
import com.cym.model.RepositoryGroup;
import com.cym.model.RepositoryUser;
import com.cym.model.User;
import com.cym.service.ConfigService;
import com.cym.service.RepositoryService;
import com.cym.service.SettingService;
import com.cym.utils.BaseController;
import com.cym.utils.BeanExtUtil;
import com.cym.utils.JsonResult;

import cn.craccd.sqlHelper.bean.Page;

@Controller
@RequestMapping("/adminPage/repository")
public class RepositoryController extends BaseController {
	@Autowired
	RepositoryService repositoryService;
	@Autowired
	ConfigService configService;
	@Autowired
	SettingService settingService;

	@RequestMapping("")
	public ModelAndView index(HttpServletRequest request, ModelAndView modelAndView, Page page, String keywords) {
		String port = settingService.get("port");

		page = repositoryService.search(page, keywords);

		Page<RepositoryExt> pageExt = BeanExtUtil.copyPageByProperties(page, RepositoryExt.class);
		for (RepositoryExt repositoryExt : (List<RepositoryExt>) pageExt.getRecords()) {
			String url = "svn://" + getIP(request.getRequestURL().toString() + "/");
			if (!port.equals("3690")) {
				url += (":" + port);
			}
			url += "/" + repositoryExt.getName();
			repositoryExt.setUrl(url);
		}

		modelAndView.addObject("userList", sqlHelper.findAll(User.class));
		modelAndView.addObject("groupList", sqlHelper.findAll(Group.class));

		modelAndView.addObject("keywords", keywords);
		modelAndView.addObject("page", pageExt);
		modelAndView.setViewName("/adminPage/repository/index");
		return modelAndView;
	}

	public static String getIP(String url) {
		URI uri = null;
		try {
			uri = new URI(url);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return uri.getHost();
	}

	@Transactional
	@RequestMapping("addOver")
	@ResponseBody
	public JsonResult addOver(Repository repository) {
		Repository repositoryOrg = repositoryService.getByName(repository.getName(), repository.getId());
		if (repositoryOrg != null) {
			return renderError("此仓库名已存在");
		}

		repositoryService.insertOrUpdate(repository);
		configService.refresh();
		return renderSuccess();
	}

	@RequestMapping("detail")
	@ResponseBody
	public JsonResult detail(String id) {
		Repository repository = sqlHelper.findById(id, Repository.class);
		return renderSuccess(repository);
	}

	@Transactional
	@RequestMapping("del")
	@ResponseBody
	public JsonResult del(String id) {
		repositoryService.deleteById(id);
		configService.refresh();
		return renderSuccess();
	}

	@RequestMapping("userPermission")
	public ModelAndView userPermission(HttpServletRequest request, ModelAndView modelAndView, Page page, String repositoryId) {

		page = repositoryService.userPermission(page, repositoryId);

		Page<RepositoryUserExt> pageExt = BeanExtUtil.copyPageByProperties(page, RepositoryUserExt.class);
		for (RepositoryUserExt repositoryUserExt : (List<RepositoryUserExt>) pageExt.getRecords()) {
			repositoryUserExt.setUser(sqlHelper.findById(repositoryUserExt.getUserId(), User.class));
		}

		modelAndView.addObject("userList", sqlHelper.findAll(User.class));

		modelAndView.addObject("repositoryId", repositoryId);
		modelAndView.addObject("page", pageExt);
		modelAndView.setViewName("/adminPage/repository/userPermission");
		return modelAndView;
	}

	@Transactional
	@RequestMapping("addUser")
	@ResponseBody
	public JsonResult addUser(RepositoryUser repositoryUser) {
		if (repositoryService.hasUser(repositoryUser.getUserId(), repositoryUser.getRepositoryId())) {
			return renderError("该用户已存在");
		}
		repositoryService.addUser(repositoryUser);
		configService.refresh();
		return renderSuccess();
	}

	@Transactional
	@RequestMapping("delUser")
	@ResponseBody
	public JsonResult delUser(String id) {
		repositoryService.delUser(id);
		configService.refresh();
		return renderSuccess();
	}

	@Transactional
	@RequestMapping("userDetail")
	@ResponseBody
	public JsonResult userDetail(String id) {

		return renderSuccess(sqlHelper.findById(id, RepositoryUser.class));
	}

	@RequestMapping("groupPermission")
	public ModelAndView groupPermission(HttpServletRequest request, ModelAndView modelAndView, Page page, String repositoryId) {

		page = repositoryService.groupPermission(page, repositoryId);

		Page<RepositoryGroupExt> pageExt = BeanExtUtil.copyPageByProperties(page, RepositoryGroupExt.class);
		for (RepositoryGroupExt repositoryGroupExt : (List<RepositoryGroupExt>) pageExt.getRecords()) {
			repositoryGroupExt.setGroup(sqlHelper.findById(repositoryGroupExt.getGroupId(), Group.class));
		}

		modelAndView.addObject("groupList", sqlHelper.findAll(Group.class));

		modelAndView.addObject("repositoryId", repositoryId);
		modelAndView.addObject("page", pageExt);
		modelAndView.setViewName("/adminPage/repository/groupPermission");
		return modelAndView;
	}

	@Transactional
	@RequestMapping("addGroup")
	@ResponseBody
	public JsonResult addGroup(RepositoryGroup repositoryGroup) {
		if (repositoryService.hasGroup(repositoryGroup.getGroupId(), repositoryGroup.getRepositoryId())) {
			return renderError("该小组已存在");
		}

		repositoryService.addGroup(repositoryGroup);
		configService.refresh();
		return renderSuccess();
	}

	@Transactional
	@RequestMapping("delGroup")
	@ResponseBody
	public JsonResult delGroup(String id) {
		repositoryService.delGroup(id);
		configService.refresh();
		return renderSuccess();
	}

	@Transactional
	@RequestMapping("groupDetail")
	@ResponseBody
	public JsonResult groupDetail(String id) {

		return renderSuccess(sqlHelper.findById(id, RepositoryGroup.class));
	}
}
